package com.china08.yxyapi.api.temp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

@CrossOrigin
@RestController
public class LectureResResSchoolErrorUpd extends YxyApiBaseController {
    private static final Logger log = LoggerFactory.getLogger(LectureResResSchoolErrorUpd.class);

    @Autowired
    private MongoTemplate mt;

    @Value("${yx.rest.api.yx.path}")
    private String yxpath;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/api_yw/temp/lectureres/error/lectureresschool", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response) {
        StringBuffer subBuffer = new StringBuffer();
        log.info("处理备授课 错误字段 res_school");
        //
        DBCollection collection = mt.getCollection(mt.getCollectionName(YxyLectureRes.class));
        DBObject query = new BasicDBObject("$where", "this.school_id == this.res_school");
        @SuppressWarnings("unchecked")
        List<String> list = collection.distinct("school_id", query);
        log.info("总共处理学校:" + list.size());
        //
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("schoolIds", list);
        JSONObject jsonObject = new JSONObject(map);
        HttpEntity<String> formHttpEntity = new HttpEntity<>(jsonObject.toString(), headers);
        @SuppressWarnings("unchecked")
        //
        List<Map<String, Object>> httpRespSchool = restTemplate.postForObject(yxpath + "/comm/school/byschoolIds",
                formHttpEntity, List.class);
        log.info("可以查询到学校昵称的共：" + httpRespSchool.size());
        //
        for (Map<String, Object> map2 : httpRespSchool) {
            String schoolId = map2.get("schoolId") + "";
            String schoolNick = map2.get("schoolNick") + "";
            if (StringUtils.isNotBlank(schoolId)) {
                subBuffer.append("正在处理： " + schoolId + " 名称为：" + schoolNick);
                subBuffer.append("\n");
                log.info("正在处理： " + schoolId + " 名称为：" + schoolNick);
                Query updateQuery = new Query();
                updateQuery.addCriteria(Criteria.where("resSchool").is(schoolId));
                Update update = new Update();
                update.set("resSchool", schoolNick);
                mt.updateMulti(updateQuery, update, YxyLectureRes.class);
            }
        }
        log.info("处理备授课错误字段res_school 结束");
        return subBuffer;
    }

    protected static class RespModel {
        public String schoolId;
        public String schoolNick;
    }
}
